
/**行为数据仓库 */
export class BehaviorDataStore {
}
/**行为基类 */
export abstract class BaseBehavior<T extends BehaviorDataStore>{
    /**生命周期 end*/
    abstract id: number;
    /**执行次数 */
    public times: number = 0;
    /**执行开始时间 */
    public startTime: number = -1;
    /**单次执行结束时间 */
    public endTime: number = -1;
    /**行为管理器中心 */
    private _mgr: BehaviorCenter<T>;
    constructor(mgr: BehaviorCenter<T>) {
        this._mgr = mgr;
    }

    /**生命周期 start*/
    protected onStart: () => void;
    /**
     * 每次刷新
     * @param time 从执行到过去的时间
     */
    protected onUpdate: (time:number) => void;
    /**生命周期 结束 */
    protected onEnd: () => void
    /**开始 */
    public _start() {
        this.times++;
        this.startTime = Date.now();
        this.endTime = -1;
        this.onStart && this.onStart();
    }
    /**结束 */
    public _end() {
        this.endTime = Date.now();
        this.onEnd && this.onEnd();
    }
    /**获取执行时间 */
    public getRunTime(): number {
        if (this.endTime === -1) {
            return -1;
        }
        return this.endTime - this.startTime;
    }
    /**返回跳转id数组 */
    abstract getJumpIdArr(): number[];
    private _checkJumpId(id: number): boolean {
        return this.getJumpIdArr().includes(id)
    }
    /**获取共享仓库 */
    protected get store(): T {
        return this._mgr.store;
    }
    /**跳转到指定id */
    protected jump(id: number) {
        if (!this._checkJumpId(id)) {
            throw "未发现即将跳转的id" + id;
        }
        this._mgr.jumpBehaver(id);
    }
    public update(curTime:number) {
        this.onUpdate && this.onUpdate(curTime-this.startTime);
    }
}
/**行为中心 */
export class BehaviorCenter<T extends BehaviorDataStore>{
    private _map: Map<number, BaseBehavior<T>> = new Map();
    /**行为中心数据仓库，存在于整个行为执行时 */
    private _store: T = new BehaviorDataStore() as T;
    private _current: BaseBehavior<T>;
    public get store(): T {
        return this._store
    }
    /**设置仓库 */
    public setStore(data: T) {
        this._store = data;
    }
    /**添加行为 */
    public registBehavior<U extends BaseBehavior<T>>(behavior: new (mgr: BehaviorCenter<T>) => U) {
        let b = new behavior(this)
        this._map.set(b.id, b);
    }
    /**跳转行为 */
    public jumpBehaver(id: number) {
        let b = this._map.get(id)
        this.changeCurrent(b);
    }
    private changeCurrent(b: BaseBehavior<T>) {
        if (this._current) {
            this._current._end();
        }
        this._current = b;
        b._start();
    }
    /**刷新 */
    public update() {
        this._current.update(Date.now());
    }
}
// function test() {
//     enum BEHAVER {
//         TEST1 = 1001,
//         TEST2 = 1002,
//         TEST3 = 1003,
//         TEST4 = 1004,
//     }
//     class TestStore extends BehaviorDataStore {
//         data = {
//             aaa: 1
//         }
//     }
//     class Test1 extends BaseBehavior<TestStore>{
//         id: number = BEHAVER.TEST1;
//         getJumpIdArr(): number[] {
//             return [
//                 BEHAVER.TEST2
//             ]
//         }
//         protected onStart: () => void = () => {
//             console.log("Test1 on start")
//             console.log(this.store)
//             console.log(this.store.data.aaa)
//             this.store.data.aaa++;
//             setTimeout(() => {
//                 this.jump(BEHAVER.TEST2)
//             }, 2000);
//         };
//         protected onEnd: () => void = () => {
//             console.log("Test1 on End")
//         };
//     }

//     class Test2 extends BaseBehavior<TestStore>{
//         id: number = BEHAVER.TEST2;
//         getJumpIdArr(): number[] {
//             return [
//                 BEHAVER.TEST3
//             ]
//         }
//         protected onStart: () => void = () => {
//             console.log("Test2 on start")
//             console.log(this.store.data.aaa)
//             this.store.data.aaa++;
//             setTimeout(() => {
//                 this.jump(BEHAVER.TEST3)
//             }, 2000);
//         };
//         protected onEnd: () => void = () => {
//             console.log("Test2 on End")
//         };
//     }
//     class Test3 extends BaseBehavior<TestStore>{
//         id: number = BEHAVER.TEST3;
//         getJumpIdArr(): number[] {
//             return [
//                 BEHAVER.TEST1
//             ]
//         }
//         protected onStart: () => void = () => {
//             console.log("Test3 on start")
//             console.log(this.store.data.aaa)
//             this.store.data.aaa++;
//             setTimeout(() => {
//                 this.jump(BEHAVER.TEST1)
//             }, 2000);
//         };
//         protected onEnd: () => void = () => {
//             console.log("Test3 on End")
//         };
//     }

//     let center: BehaviorCenter<TestStore> = new BehaviorCenter();
//     center.setStore(new TestStore())
//     center.registBehavior(Test1)
//     center.registBehavior(Test2)
//     center.registBehavior(Test3)
//     center.jumpBehaver(BEHAVER.TEST1)
// }

